//
//  SJFollowCell.m
//  WSL
//
//  Created by CJ on 2017/12/5.
//  Copyright © 2017年 sanjuetech. All rights reserved.
//

#import "SJFollowCell.h"
#import "SJUserInfoItem.h"

@interface SJFollowCell ()

@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (strong, nonatomic) UIButton *followButton;

@end

@implementation SJFollowCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setUserInfo:(SJUserInfoItem *)userInfo {
    
    _userInfo = userInfo;
    
    [self.avatarImageView setImageWithURL:[NSURL URLWithString:userInfo.avatar] placeholder:[UIImage imageNamed:@"common_icon_defaultavatar"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    self.nameLabel.text = userInfo.name;
    
    if ([userInfo.st isEqualToString:@"3"] || [userInfo.st isEqualToString:@"1"]) {
        self.followButton.selected = YES;
    } else {
        self.followButton.selected = NO;
    }
}

- (void)setIsMe:(BOOL)isMe {
    
    _isMe = isMe;
    
    if (isMe) {
        self.followButton.hidden = NO;
    } else {
        self.followButton.hidden = YES;
    }
}

- (void)setupUI {
    
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
    [self.contentView addSubview:self.avatarImageView];
    self.avatarImageView.sd_layout
    .leftSpaceToView(self.contentView, SJLayoutFrame(10))
    .centerYEqualToView(self.contentView)
    .heightIs(SJLayoutFrame(60))
    .widthIs(SJLayoutFrame(60));

    
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.sd_layout
    .centerYEqualToView(self.contentView)
    .leftSpaceToView(self.avatarImageView, SJLayoutFrame(15))
    .heightIs(SJLayoutFrame(18));
    [self.nameLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW * .5];
    
    
    [self.contentView addSubview:self.followButton];
    self.followButton.sd_layout
    .centerYEqualToView(self.contentView)
    .rightSpaceToView(self.contentView, SJLayoutFrame(10))
    .heightIs(SJLayoutFrame(30))
    .widthIs(SJLayoutFrame(60));
    
}


#pragma mark - getter

- (UIView *)bgView {
    
    if (!_bgView) {
        
        _bgView = [UIView new];
        _bgView.sd_cornerRadius = @(SJCornerRadius);
        _bgView.backgroundColor = SJColorHexString(@"#ffffff");
        _bgView.alpha = .1;
    }
    return _bgView;
}

- (UILabel *)nameLabel{
    
    if (!_nameLabel) {
        
        _nameLabel = [UILabel new];
        _nameLabel.textColor = SJColorHexString(@"#ffffff");
        _nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(18)];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}


- (UIImageView *)avatarImageView{
    
    if (!_avatarImageView) {
        
        _avatarImageView = [UIImageView new];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageView.sd_cornerRadiusFromHeightRatio = @(0.5);
        _avatarImageView.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
        _avatarImageView.layer.borderWidth = SJLayoutFrame(2);//边框宽度
        
    }
    return _avatarImageView;
}

- (UIButton *)followButton {
    
    if (!_followButton) {
        
        _followButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_followButton setImage:[UIImage imageNamed:@"Personal_icon_followe"] forState:UIControlStateNormal];
        [_followButton setImage:[UIImage imageNamed:@"Personal_icon_followed"] forState:UIControlStateSelected];
        [_followButton addTarget:self action:@selector(followAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _followButton;
}

- (void)followAction {
    
    if (self.followTap) {
        self.followTap(self);
    }
}

@end
